package com.qf.service.impl;/*
 *徐韬
 *2021/11/5
 *8:17
 */

import com.qf.constons.resultCode;
import com.qf.events.updateParamevents;
import com.qf.exceptions.updateException;
import com.qf.mapper.paramMapper;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.Params;
import com.qf.service.paramsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class paramsServiceimpl implements paramsService {
    private paramMapper paramsMapper;
    private ApplicationContext context;
@Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setParamsMapper(paramMapper paramsMapper) {
        this.paramsMapper = paramsMapper;
    }

    @Override
    public void addParams(Params params) {
     paramsMapper.insertParams(params);
     context.publishEvent(new updateParamevents());

    }

    @Override
    public void updateParams(Params params) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser =(MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type != 1){
            throw new updateException("你没有权限修改该调试据", resultCode.NO_ROOT);
        }
        paramsMapper.updateParams(params);
        context.publishEvent(new updateParamevents());
    }

    @Override
    public void deleteParams(List<Integer> names) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type!=1){
            throw new updateException("你没有权限删除该条数据", resultCode.NO_ROOT);
        }

        paramsMapper.delete(names);
        context.publishEvent(new updateParamevents());
    }
}
